angular
		.module('controller.account', [ 'services', 'ui.router' ])
		.controller(
				'accountCtrl',
				[
						'$scope',
						'$window',
						'$state',
						'apiService',
						'constants',
						'notificationService',
						'alertManager',
						function($scope, $window, $state, apiService,
								constants, notificationService, alertManager) {
							$scope.signin = function() {
								if (!$scope.username || !$scope.password)
									return;
								var account = {
									username : $scope.username,
									password : $scope.password
								}
								apiService
										.signin(account)
										.then(
												function(response) {
													if (response.token) {
														$window.localStorage[constants.authTokenName] = "Bearer "
																+ response.token;
														$window.localStorage[constants.username] = "feastBooking_"
																+ $scope.username;
														$window.localStorage[constants.notificationToken] = hex_md5($scope.password);
														$state
																.go(constants.hotelsStatePrefix
																		+ '.'
																		+ constants.hotelsListState);
													}
												}, function(response) {
													alert("");
												})
							}
							$scope.signout = function() {
								alertManager.addAlert('danger', '用戶名或密碼不正確');
							}
						} ])